package com.fjnu.dao.impl;

import com.fjnu.annotation.NPECatch;
import com.fjnu.dao.UserDao;
import com.fjnu.entity.User;
import lombok.Builder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


/**
 * @desc:
 * @author:
 * @date:
 */

@NPECatch
@Repository
public class UserDaoImpl implements UserDao {
    /**
     * 注入 connection Bean,利用connection bean来完成对数据库的操作
     */
    @Autowired
    private Connection connection;
    @Override
    public int insert(User user) {
        String sql = "insert into user (user_id, username, password, salt, nickname, email) values(?,?,?,?,?,?)";
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, user.getUserId());
            ps.setString(2, user.getUsername());
            ps.setString(3, user.getPassword());
            ps.setString(4, user.getSalt());
            ps.setString(5, user.getNickname());
            ps.setString(6, user.getEmail());
            return ps.executeUpdate();
        } catch (SQLException e) {
           e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(String userId) {
        return 0;
    }

    @Override
    public int updateById(String userId, User newUser) {
        return 0;
    }

    @Override
    public int updatePasswordById(String userId, String oldPassword, String newPassword) {
        return 0;
    }

    @Override
    public User selectById(String userId) {
        return null;
    }

    @Override
    public User selectByUsernameAndPassword(String username, String password) {
        String sql = "select * from user where username = ? and password = ?";
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
            ResultSet rs = ps.executeQuery(); // 执行查询
            if (rs.next()) { // 处理结果集
                return User.builder()
                        .userId(rs.getString("user_id"))
                        .username(rs.getString("username"))
                        .password(rs.getString("password"))
                        .salt(rs.getString("salt"))
                        .nickname(rs.getString("nickname"))
                        .email(rs.getString("email"))
                        .phone(rs.getString("phone"))
                        .sex(rs.getInt("sex"))
                        .age(rs.getInt("age"))
                        .build();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int selectCount() {
        String sql = "select count(*) from user";
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet rs = ps.executeQuery(); // 执行查询
            if (rs.next()) { // 处理结果集
                return rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0; // 需要添加默认返回值
    }

}
